﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Rubik.Common
{
    public class Cube
    {
        private const byte TopValue = 1;
        private const byte LeftValue = 2;
        private const byte FrontValue = 3;
        private const byte RightValue = 4;
        private const byte BottomValue = 5;
        private const byte BackValue = 6;

        public const int Scale = 3;

        private CubeElement[,,] elements = new CubeElement[Scale, Scale, Scale];

        public CubeElement[,,] Elements
        {
            get
            {
                return elements;
            }
            set
            {
                elements = value;
            }
        }

        public void Initialize()
        {
            for (int i = 0; i < Scale; i++)
            {
                for (int j = 0; j < Scale; j++)
                {
                    for (int k = 0; k < Scale; k++)
                    {
                        elements[i, j, k] = new CubeElement();
                    }
                }
            }

            // Fill Top
            for (int i = 0; i < Scale; i++)
            {
                for (int j = 0; j < Scale; j++)
                {
                    elements[i, 0, j].Top = TopValue;
                }
            }

            // Fill Left
            for (int i = 0; i < Scale; i++)
            {
                for (int j = 0; j < Scale; j++)
                {
                    elements[0, i, j].Left = LeftValue;
                }
            }

            // Fill Front
            for (int i = 0; i < Scale; i++)
            {
                for (int j = 0; j < Scale; j++)
                {
                    elements[i, j, 0].Front = FrontValue;
                }
            }

            // Fill Right
            for (int i = 0; i < Scale; i++)
            {
                for (int j = 0; j < Scale; j++)
                {
                    elements[Scale - 1, i, j].Right = RightValue;
                }
            }

            // Fill Bottom
            for (int i = 0; i < Scale; i++)
            {
                for (int j = 0; j < Scale; j++)
                {
                    elements[i, Scale - 1, j].Bottom = BottomValue;
                }
            }

            // Fill Back
            for (int i = 0; i < Scale; i++)
            {
                for (int j = 0; j < Scale; j++)
                {
                    elements[i, j, Scale - 1].Back = BackValue;
                }
            }
        }

        public byte[][,] Dump()
        {
            byte[][,] data = new byte[6][,];
            for (int i = 0; i < 6; i++)
            {
                data[i] = new byte[Scale, Scale];
            }

            // Dump Top
            for (int i = 0; i < Scale; i++)
            {
                for (int j = 0; j < Scale; j++)
                {
                    data[0][i, j] = elements[i, 0, Scale - j - 1].Top;
                }
            }

            // Dump Left
            for (int i = 0; i < Scale; i++)
            {
                for (int j = 0; j < Scale; j++)
                {
                    data[1][i, j] = elements[0, j, Scale - i - 1].Left;
                }
            }

            // Dump Front
            for (int i = 0; i < Scale; i++)
            {
                for (int j = 0; j < Scale; j++)
                {
                    data[2][i, j] = elements[i, j, 0].Front;
                }
            }

            // Dump Right
            for (int i = 0; i < Scale; i++)
            {
                for (int j = 0; j < Scale; j++)
                {
                    data[3][i, j] = elements[Scale - 1, j, i].Right;
                }
            }

            // Dump Bottom
            for (int i = 0; i < Scale; i++)
            {
                for (int j = 0; j < Scale; j++)
                {
                    data[4][i, j] = elements[i, Scale - 1, j].Bottom;
                }
            }

            // Dump Back
            for (int i = 0; i < Scale; i++)
            {
                for (int j = 0; j < Scale; j++)
                {
                    data[5][i, j] = elements[i, Scale - j - 1, Scale - 1].Back;
                }
            }

            return data;
        }
    }
}
